<?php
  
/*分类编辑递归函数封装*/
  function editCate($data, $cate=array()){
         // 查找分类同级
    $pid = $data['pid'];
      $sql = "select * from `cate` where `pid`=$pid";
      $result = mysqli_query($GLOBALS['link'],$sql);

         // 循环将查找到的同级存储到重组数组中
         while ($arr = mysqli_fetch_assoc($result)){

             // 将查询到的临时数据存储到临时数组中
            $cate[$data['id']][] = $arr;
           } 
       
         if($pid!=0){

          // 查找父级
          $sql = "select * from `cate` where `id`=$pid";

           $result = mysqli_query($GLOBALS['link'],$sql);

           $data = mysqli_fetch_assoc($result);

           // 调用自身查找父级的同级
           $cate = editCate($data,$cate);

            // print_r(1);die;

         }
    return $cate =array_reverse($cate,true);;

  


}

/*分类列表递归*/
 
    function getCate($pid=0,$cate = array(),$level=0) {
                       $sql = "select * from `cate` where pid = $pid ";
                            // 执行语句
                         $result = mysqli_query($GLOBALS['link'],$sql);
           while($arr = mysqli_fetch_assoc($result)){

                $str = '';

                for ($i=0;$i<$level;$i++){

                    $str.='★';
                }

                $arr['name'] = $str.$arr['name'];




             //[]代表多维数组
                      $cate[]=$arr;
                        // 查询是否有子级
                        // 条件为pid=此次循环到的分类的id值
                        // $cate=$arr;
                        
                        $cate = getCate($arr['id'],$cate,$level+1);

                          }  
                          return $cate;
                 }


































































